const { ccclass, property } = cc._decorator;

@ccclass
export default class gameManager extends cc.Component {

    @property([cc.Prefab])
    monsters: cc.Prefab[] = []
    @property(cc.Node)
    stateBtn: cc.Node = undefined
    @property(cc.Node)
    pauseBtn: cc.Node = undefined
    @property(cc.Node)
    startBtn: cc.Node = undefined
    @property(cc.Node)
    bg: cc.Node = undefined
    @property(cc.Label)
    starScore: cc.Label = undefined
    @property(cc.Label)
    timeTxt: cc.Label = undefined
    @property(cc.Node)
    time: cc.Node = undefined
    @property(cc.Node)
    fail: cc.Node = undefined
    @property([cc.SpriteFrame])
    failArr: cc.SpriteFrame[] = []

    private monster: cc.Node = undefined    //怪物
    private isTurnTop: boolean = false      //怪物是不是在上方
    private isTouch: boolean = false        //是否还在点击状态
    private isPause: boolean = false        //是否暂停状态
    private noTouch: boolean = false        //设置不能点击状态(防止和暂停按钮冲突)
    public isFail: boolean = false          //是否游戏失败

    onLoad() {
        this.resize()
        this.initScene()
        this.loadMonster()
        this.addEventListen()
    }

    // 适配屏幕
    private resize(){
        // 1. 先找到 SHOW_ALL 模式适配之后，本节点的实际宽高以及初始缩放值
        let srcScaleForShowAll = Math.min(cc.view.getCanvasSize().width / this.node.width, cc.view.getCanvasSize().height / this.node.height);
        let realWidth = this.node.width * srcScaleForShowAll;
        let realHeight = this.node.height * srcScaleForShowAll;

        // 2. 基于第一步的数据，再做节点宽高适配
        this.node.width = this.node.width * (cc.view.getCanvasSize().width / realWidth);
        this.node.height = this.node.height * (cc.view.getCanvasSize().height / realHeight);
    }


    // 初始化场景
    private initScene() {
        cc.director.getCollisionManager().enabled = true
        let num = cc.sys.localStorage.getItem('star')
        if (num) {
            this.starScore.string = 'x ' + num + ''
        } else {
            this.starScore.string = 'x ' + 0 + ''
        }
        this.timeTxt.string = '0'
    }

    // 添加点击事件
    private addEventListen() {
        this.node.on('touchstart', this.touchStart, this)
        this.stateBtn.on('touchstart', this.stateChange, this)
        this.stateBtn.on('touchend', this.stateChangeEnd, this)
        this.stateBtn.on('touchcancel', this.stateChangeEnd, this)


    }

    // 加载怪物
    private loadMonster() {
        let node: cc.Node = undefined
        let monster = cc.sys.localStorage.getItem('monster')
        if (monster) {
            node = cc.instantiate(this.monsters[+monster])
        } else {
            node = cc.instantiate(this.monsters[0])
            cc.sys.localStorage.setItem('monster', 0)

        }
        node.parent = this.node
        node.x = -cc.winSize.width / 4
        node.y = -cc.winSize.height / 2 + node.height / 2
        node.getComponent('monster').init(this)
        this.monster = node
    }

    // 暂停/开始
    private stateChange() {
        this.noTouch = true
        this.isPause = !this.isPause
        this.startBtn.active = this.isPause
        this.pauseBtn.active = !this.isPause
        this.isPause ? cc.director.pause() : cc.director.resume()
    }

    // 暂停/开始状态改变结束后
    private stateChangeEnd() {
        this.noTouch = false
    }

    // 点击开始
    private touchStart() {
        if (!this.isTouch && !this.noTouch) {
            this.isTouch = true
            this.isTurnTop = !this.isTurnTop
            if (this.isTurnTop) {
                this.monster.runAction(
                    cc.sequence(
                        cc.spawn(cc.moveTo(0.5, this.monster.x, cc.winSize.height / 2 - this.monster.height / 2),
                            cc.flipY(true)),
                        cc.callFunc(() => { this.isTouch = false })
                    )
                )
            } else {
                this.monster.runAction(
                    cc.sequence(
                        cc.spawn(cc.moveTo(0.5, this.monster.x, -cc.winSize.height / 2 + this.monster.height / 2),
                            cc.flipY(false)),
                        cc.callFunc(() => { this.isTouch = false })
                    )
                )
            }
        }


    }

    start() {
        this.schedule(this.timeRun, 1, cc.macro.REPEAT_FOREVER, 0.1)
    }

    // 计时
    private timeRun() {
        this.timeTxt.string = +this.timeTxt.string + 1 + ''
    }

    // 获得星星
    public addStar() {
        let num = cc.sys.localStorage.getItem('star')
        if (num) {
            let n = +num + 1
            this.starScore.string = 'x ' + +n + ''
            cc.sys.localStorage.setItem('star', n)
        } else {
            this.starScore.string = 'x ' + 1 + ''
            cc.sys.localStorage.setItem('star', 1)
        }
    }

    // 游戏失败
    public failGame() {
        this.isFail = true
        this.stateBtn.active = false
        this.time.active = false
        this.unschedule(this.timeRun)
        let num = cc.sys.localStorage.getItem('time')
        if (num) {
            if (num < +this.timeTxt.string) {
                cc.sys.localStorage.setItem('time', +this.timeTxt.string)
            }
        }
        else {
            cc.sys.localStorage.setItem('time', +this.timeTxt.string)
        }
        this.fail.active = true
        this.fail.getComponent('fail').init()

    }
    // update (dt) {}
}
